<?php
class Administrator_CouponcodeController extends Zend_Controller_Action
{
	public function init()
	{
//		$member=new CMember();
//		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){	
//			$this->_redirect(MY_URL.'/administrator-auth-login');
//		}
//		$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
//    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
//				
//		} else {
//        	$strarray = explode(",", $memberArray[0]['menuflag']);
//        	
//        	if (!in_array('12', $strarray)) {
//        		exit('no priviage');
//        	}
//		}
	}
	
	public function preDispatch()
	{
		$this->getHelper ('layout')->setLayout('framelayout');
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
	}
	
	public function indexAction()
	{
		$page=$this->_request->getParam('page');
    	
		$couponcode = new CCouponCode();
		
		$itemCounts=$couponcode->getAllItemCounts('');
		
    	Zend_Loader::loadClass("CFilter");
    	
    	$pageArray = CFilter::filterPage ( $page, $itemCounts, 50 );
		$itemArray=$couponcode->codelist($pageArray['from'],50);
		
		Zend_Loader::loadClass("CPage");
    	
    	$pageString = CPage::divPages( $pageArray ['pageNumber'], $pageArray ['page'], ROOT_PATH."/administrator-couponcode-index?"); //生成分页字符串
    	
    	$this->view->assign ( "pageString", $pageString );
    	
    	$this->view->assign ( "pageArray", $pageArray );
		
		$this->view->assign('itemArray',$itemArray);
	}
	
	public function addAction()
	{}
	
	public function doaddAction()
	{
		$couponcode = new CCouponCode();
		$KEYWORD = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789';
		$postr = str_shuffle($KEYWORD);
		$pos = mt_rand(0,strlen($postr) - 20);
		$code = '';
		while (empty($code))
        {
            $code = substr($postr,$pos,mt_rand(15,20));
            if ($couponcode->isexist($code))
            {
                $code = '';
            }
        }
		
		$this->_helper->viewRenderer->setNoRender();
    	
        if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
    	
    	$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
		$itemarray = array(
			'code'		=>	$code,
			'from'		=>	$input->from,
			'score'		=>  $input->score,
			'createtime'=>	time(),
			'endtime'	=>  strtotime(trim($input->endtime))
		);
		$couponcode->createcode($itemarray);
		
		$this->view->showMsg("生成成功!",'administrator-couponcode-index');
		
		return true;
	}
	
	
	public function delAction()
	{
		$this->_helper->viewRenderer->setNoRender();
    	
    	$code=$this->_request->getParam('code');
    	
    	$couponcode = new CCouponCode();
    	$couponcode->codedelete($code);
		
    	$this->view->showMsg('删除成功!');
	}
	
	public function productAction()
	{
		if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		$this->view->itemArray = $this->_request->getParam('itemArray');
		
		$couponcode = new CCouponCode();
		$this->view->codeArray = $couponcode->getAllcodes();
		
	}
	
	public function doproductAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		$itemarray = $this->_request->getParam('itemArray');
		$codes = $this->_request->getParam('codes');
		if(count($codes) == 0) {
			$this->view->showMsg("发生未知错误!");
			return false;
		}
		foreach ($codes as $k=>$v)
		{
			$temp_codes_arr = split('@', $v);
			$temp_codes_arr[1] = split(',', $temp_codes_arr[1]);
			foreach ($temp_codes_arr[1] as $tk=>$tv) {
				if(empty($tv))
					unset($temp_codes_arr[1][$tk]);
			}
			$codes[$k] = $temp_codes_arr;
		}
		unset($temp_codes_arr);
		
		foreach ($itemarray as $item)
		{
			foreach ($codes as $ck=>$cv) {
				if(in_array($item, $cv[1]))
					continue;
				array_push($codes[$ck][1],$item);
			}
		}
		$couponcode = new CCouponCode();
		foreach ($codes as $cv) {
			$editdata = array(
				'itemids' => join(',', $cv[1])
			);
			$couponcode->editcode($cv[0], $editdata);
		}
		$product = new CProduct();
		$product->updateSalesStatus(1, join(',',$itemarray));
		
		$this->view->showMsg("产品促销设置成功!",'administrator-product-index');
	}
	
	public function productoutAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$itemarray = $this->_request->getParam('itemArray');
		
		$couponcode = new CCouponCode();
		$codes = $couponcode->getAllcodes();
		foreach ($codes as $cv) {
			$editdata = array(
				'itemids' => join(',', array_diff(split(',',$cv['itemids']),$itemarray))
			);
			$couponcode->editcode($cv['code'], $editdata);
		}
		$product = new CProduct();
		$product->updateSalesStatus(0, join(',',$itemarray));
		$this->view->showMsg("指定产品已退出促销!",'administrator-product-index');
	}
	
	public function editAction()
	{
		$code = $this->_request->getParam('code');
		if($code == '') {
			$this->view->showMsg("发生未知错误!");
			return false;
		}
		$couponcode = new CCouponCode();
		$this->view->editdata = $couponcode->isexist($code);
		
		$couponCodeUse = new CCouponCodeUse();
		$this->view->isUsed = $couponCodeUse->isexist($code);
	}
	
	public function doeditAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		if(!$this->getRequest()->isPost()){
			
			$this->view->showMsg("发生未知错误!");
			
			return false;
			
		}
		
		$thisArray=$this->_request->getParams();
    	
		$filters = array(
    		'*'   => 'StringTrim'
		);

		$validators = array(
		);
		
		$input = new Zend_Filter_Input($filters, $validators, $thisArray);
    	
		$itemarray = array(
			'from'		=>	$input->from,
			'endtime'	=>  strtotime(trim($input->endtime)),
			'score'		=>  $input->score
		);
		$couponcode = new CCouponCode();
		$couponcode->editcode($input->code, $itemarray);
		
		$this->view->showMsg("编辑成功!",'administrator-couponcode-index');
		
		return true;
	}
}
